package com.xx.job.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.job.controller.dto.PageDTO;
import com.xx.job.entity.system.SysPost;
import com.xx.job.mapper.SysPostMapper;
import com.xx.job.service.system.SysPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangcy
 * @since 2021-01-14
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    @Autowired
    private SysPostMapper sysPostMapper;

    public List<SysPost> sysPostList(){
       return sysPostMapper.selectList(null);
    }

    @Override
    public Map<String,Object> selectPagePost(PageDTO pageDTO, SysPost sysPost) {
        Map<String,Object> data = new HashMap<>();
        IPage<SysPost> page = new Page<>(pageDTO.getPageIndex(),pageDTO.getPageSize());
        QueryWrapper<SysPost> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(sysPost.getPostCode())){
            wrapper.like("post_code",sysPost.getPostCode().trim());
        }
        if(StringUtils.isNotBlank(sysPost.getPostName())){
            wrapper.like("post_name",sysPost.getPostName().trim());
        }
        if(StringUtils.isNotBlank(sysPost.getStatus())){
            wrapper.like("status",sysPost.getPostCode().trim());
        }
        IPage<SysPost> sysPostIPage = sysPostMapper.selectPage(page, wrapper);
        data.put("count",sysPostIPage.getTotal());
        data.put("list",sysPostIPage.getRecords());
        data.put("pageIndex",pageDTO.getPageIndex());
        data.put("pageSize", pageDTO.getPageSize());
        return data;
    }

    @Override
    public int add(SysPost sysPost) {
        return sysPostMapper.insert(sysPost);
    }

    @Override
    public int delete(Long id) {
        return sysPostMapper.deleteById(id);
    }

    @Override
    public int update(SysPost sysPost) {
        return sysPostMapper.updateById(sysPost);
    }

    @Override
    public SysPost select(Long id) {
        return sysPostMapper.selectById(id);
    }

}
